//
//  HHMoreClassViewController.m
//  Flower
//
//  Created by 花花 on 2017/1/17.
//  Copyright © 2017年 花花. All rights reserved.
//

#import "HHMoreClassViewController.h"
#import "UIScrollView+HeaderScaleImage.h"
#import "HHThemeViewController.h"
#import "HHClearCell.h"
#import "UIImageView+WebCache.h"
@interface HHMoreClassViewController ()

@property(nonatomic, strong) NSArray *DataArr;
@property(nonatomic, strong) HHClearCell *clearCell;

@end

static NSString *const cellID = @"cellID";
@implementation HHMoreClassViewController

#pragma mark - Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self  setupTableView];
    
    self.DataArr = @[@"个性皮肤",@"清除缓存",@"test"];
    
    
    
}
-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [self.tableView reloadData];
    
}

#pragma mark - OBJMethod
- (void)setupTableView{
    
    
    self.tableView =[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.yhp_headerScaleImage = [UIImage imageNamed:@"header"];
    UIView *headerView = [[UIView alloc]init];
    self.tableView.yhp_headerScaleImageHeight = 300;
    headerView.frame = CGRectMake(0, 0, 0, self.tableView.yhp_headerScaleImageHeight);
    headerView.backgroundColor = [UIColor clearColor];
    self.tableView.tableHeaderView = headerView;
    [self.tableView registerNib:[UINib nibWithNibName:@"HHClearCell" bundle:nil] forCellReuseIdentifier:cellID];
    
    
}


#pragma mark - 计算缓存大小
- (NSString *)getCacheSize {
    //定义变量存储总的缓存大小
    long long sumSize = 0;
    
    //01.获取当前图片缓存路径
    NSString *cacheFilePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
    
    //02.创建文件管理对象
    NSFileManager *filemanager = [NSFileManager defaultManager];
    
    //获取当前缓存路径下的所有子路径
    NSArray *subPaths = [filemanager subpathsOfDirectoryAtPath:cacheFilePath error:nil];
    
    //遍历所有子文件
    for (NSString *subPath in subPaths) {
        //1）.拼接完整路径
        NSString *filePath = [cacheFilePath stringByAppendingFormat:@"/%@",subPath];
        //2）.计算文件的大小
        long long fileSize = [[filemanager attributesOfItemAtPath:filePath error:nil]fileSize];
        //3）.加载到文件的大小
        sumSize += fileSize;
    }
    float size_m = sumSize/(1000*1000);
    return [NSString stringWithFormat:@"%.2fMB",size_m];
    
}
#pragma mark - <UITableViewDataSource>
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.DataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HHClearCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    NSString *text;
    if (indexPath.row == 0) {
        text = @"";
    }else if (indexPath.row ==1){
        
        text = [self getCacheSize];
        
    }
    cell.subLabel.text = text;
    cell.titleLabel.text = self.DataArr[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.row == 0) {
        HHThemeViewController *themeVc =[[HHThemeViewController alloc]init];
        [self.navigationController pushViewController:themeVc animated:YES];
        
    }else if(indexPath.row == 1){
        
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"你确定要清空缓存吗" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        
        [alert showAlertWithHandler:^(NSInteger buttonIndex) {
            if (buttonIndex == 1) {
                //01......
                
                
                NSFileManager *fileManager = [NSFileManager defaultManager];
                //02.....
                NSString *cacheFilePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
                //03......
                [fileManager removeItemAtPath:cacheFilePath error:nil];
                
                //04刷新第一行单元格
                NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
                [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
                
                //05 ：04和05使用其一即可
                [self.tableView reloadData];//刷新表视图
            }
        }];
        
    }else if (indexPath.row == 2){//UIAlertController设置按钮颜色
        //iOS 8.3 之前没有此属性，如果需要支持iOS8.3以下需要判断以下是否存在该属性，否则会发生崩溃~
        UIAlertController *alertVc =[UIAlertController alertControllerWithTitle:@"温馨提示" message:@"你是逗逼中的战斗机吗" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *cancelAction =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            HHLog(@"小小的逗逼");
        }];
        [cancelAction setValue:[UIColor redColor] forKey:@"titleTextColor"];
        UIAlertAction *finishAction =[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [finishAction setValue:[UIColor greenColor] forKey:@"titleTextColor"];
        [alertVc addAction:cancelAction];
        [alertVc addAction:finishAction];
        
        [self presentViewController:alertVc animated:YES completion:nil];
    }
}


@end
